﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Controls;

namespace ReportingManagement.Components.Validation
{
    public class RegexValidationRule : ValidationRule
    {
        private string expression;
        public string Expression
        {
            get { return expression; }
            set { expression = value; }
        }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (string.IsNullOrWhiteSpace(expression)) return new ValidationResult(true, null);

            string input = (string)value;
            bool matched = Regex.IsMatch(input, expression);
            string errorContent = matched ? "The value does not agree with the validation rule" : null;
            return new ValidationResult(matched, errorContent);
        }
    }
}
